<template>
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-gradual-pink" :isBack="false"><block slot="content">发布活动</block></cu-custom>
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action">
					<text class="cuIcon-title text-blue "></text> 活动信息
				</view>
			</view>
			<view class="padding" v-if="isAdmin">
				<form>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">活动名称：</view>
						<input v-model="form.title" placeholder="请输入活动名称" name="input"></input>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">游戏大区：</view>
						<picker @change="bindRegionalChange" :value="regionalIndex" :range="regionalArray" placeholder="请选择大区">
							<view class="uni-input">{{regionalArray[regionalIndex]}}</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">活动类型：</view>
						<picker @change="bindTypeChange" :value="typeIndex" :range="typeArray" placeholder="请选择活动类型">
							<view class="uni-input">{{typeArray[typeIndex]}}</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">开始日期：</view>
						<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
							<view class="uni-input">{{date}}</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">开始时间：</view>
						<picker mode="time" :value="time" start="00:01" end="23:59" @change="bindTimeChange">
							<view class="uni-input">{{time}}</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">活动说明：</view>
						<input v-model="form.memo" placeholder="请输入活动说明" name="input"></input>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">蓝方队伍名：</view>
						<input v-model="form.blueRanksName" placeholder="请输入蓝方队伍名" name="input"></input>
					</view>
					<view class="cu-form-group">
						<view class="title zy-required text-bold">红方队伍名：</view>
						<input v-model="form.redRanksName" placeholder="请输入红方队伍名" name="input"></input>
					</view>
				</form>
				<button class="cu-btn block bg-blue margin-tb-sm lg" type="" @click="saveActivy">发布活动</button>
			</view>
			<view class="padding" v-else>
				你暂时没有权限发布活动哦
			</view>
		</scroll-view>
	</view>
	
				
</template>

<script>
	
	import * as ResponseStatus from '@/common/response-status.js'
	import {
		BASE_URL,
		doPostForm,
		doPostJson,
		showInfoToast,
		doGet,
		nullToStr,
		IS_ADMIN
	} from '@/common/util.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				form: {
				    title: null,
					regional: '黑色玫瑰',
					type: '5V5征召',
					status: '报名中',
					memo: '欢迎大家积极报名参与活动',
					startDate: null,
					userId: null,
					blueRanksName: null,
					redRanksName: null
				},
				typeIndex: 0,
				typeArray: ['5V5征召', '5V5自选'],
				date: currentDate,
				time: '21:00',
				isAdmin: false,
				regionalIndex: 0,
				regionalArray: ['黑色玫瑰', '艾欧尼亚']
			}
		},
		mounted() {
			this.isAdmin = uni.getStorageSync(IS_ADMIN)
		},
		methods: {
			bindTypeChange: function(e) {
				this.typeIndex = e.target.value
				this.form.type = this.typeArray[this.typeIndex]
			},
			bindDateChange: function(e) {
				this.date = e.target.value
			},
			bindTimeChange: function(e) {
				this.time = e.target.value
			},
			/** 大区下拉框绑定 */
			bindRegionalChange: function(e) {
				this.regionalIndex = e.target.value
				this.form.regional = this.regionalArray[this.regionalIndex]
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			/** 保存活动 */
			saveActivy() {
				if (!this.form.title) {
					showInfoToast("请输入活动名称")
					return
				}
				if (!this.form.memo) {
					showInfoToast("请输入活动说明")
					return
				}
				this.form.startDate = this.date + ' ' + this.time
				let data = this.form
				doPostJson('/activity/user/save', data, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						showInfoToast('发布成功');
						this.form = {
							title: null,
							regional: '黑色玫瑰',
							type: '5V5征召',
							status: '报名中',
							memo: '欢迎大家积极报名参与活动',
							startDate: null,
							userId: null
						}
						this.date = this.getDate({
							format: true
						}),
						this.time = '21:00'
					} else {
						showInfoToast(res.data.message);
					}
				})
			}
		}
	}
</script>

<style>

</style>
